/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-22 17:04:39
 */
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import checker from 'vite-plugin-checker'
const path = require('path')
import packageJson from './package.json'
import { ConfigEnv } from 'vite'
export default ({ mode }: ConfigEnv) => {
	const server =
		mode === 'development'
			? {
				port: 8080,
				proxy: {
					'/serverApi': {
						target: 'http://localhost:3003/',
						changeOrigin: true,
						rewrite: (path: string) => path.replace(/^\/serverApi/, ''),
					},
				},
				headers: {
					'Access-Control-Allow-Origin': '*',
				},
				disableHostCheck: true,
				host: '0.0.0.0',
				cors: true,
			}
			: {}

	return {
		base: '/',
		root: './', // index.html 文件所在的位置
		envDir: './env', // 加载 .env 文件的目录
		plugins: [
			vue(),
			vueJsx(),
			checker({
				typescript: {
					tsconfigPath: path.resolve(__dirname, './tsconfig.json'),
				},
			}),
		],
		css: {
			modules: {
				scopeBehaviour: 'local',
			},
			preprocessorOptions: {
				less: {
					javascriptEnabled: true,
				},
				scss: {
					// 引入scss全局变量 给导入的路径最后加上;
					// additionalData: `@import "./src/style/app.scss";@import "./src/style/variables.scss";`,
					javascriptEnabled: true,
				},
			},
		},
		define: {
			'process.env': {
				NODE_ENV: mode === 'beta' ? 'production' : mode,
				APP_NAME: packageJson.name,
			},
		},
		resolve: {
			alias: {
				'@': path.resolve(__dirname, 'src'),
			},
			extensions: ['.tsx', '.ts', '.js', 'jsx', '.mjs'],
		},
		server,
		build: {
			target: 'modules', // modules es6 es2015  esnext
			minify: 'terser', // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
			manifest: false, // 是否产出maifest.json
			sourcemap: mode === 'beta', // 是否产出soucemap.json
			outDir: './dist', // 产出目录
			//	rollupOptions
		},
		optimizeDeps: {
			include: ['vue', 'vue-router'],
		},
	}
}
